package com.cjf.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjf.comon.BaseContext;
import com.cjf.comon.R;
import com.cjf.entity.Employee;
import com.cjf.entity.OrderDetail;
import com.cjf.entity.Orders;
import com.cjf.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    /***
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {

        orderService.submit(orders);
        return R.success("下单成功！");
    }

    /***
     * 前台 根据用户id查询订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public  R<Page> page(int page,int pageSize) {

        log.info("page={},pageSize={}",page,pageSize);

        Page pageInfo = new Page(page,pageSize);

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());

        orderService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    /***
     * 后台 查询全部订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public R<Page> pageAll(int page,int pageSize) {
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /***
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> update( @RequestBody Orders orders) {
        orderService.updateById(orders);
        return R.success("订单状态修改成功！");
    }


}
